package com.hqyj.controller;

import com.hqyj.pojo.Building;
import com.hqyj.pojo.Employee;
import com.hqyj.pojo.PageData;
import com.hqyj.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    //获取全部员工信息
    /*@RequestMapping("/getEmployee")
    public String getAll(ModelMap modelMap) {
        List<Employee> all = employeeService.getAllEmployee();
        System.out.println(all);
        modelMap.put("EMPLOYEE", all);
        return "employee/list";
    }*/

    //添加员工信息
    @RequestMapping("/addEmployee")
    @ResponseBody
    public Map<String, String> addEmployee(Employee employee) {
        int i = employeeService.insertEmployee(employee);
        Map<String, String> map = new HashMap<>();
        if (i != 0) {
            map.put("msg", "ok");
        } else {
            map.put("msg", "no");

        }
        return map;
    }

    //修改员工信息
    @RequestMapping("/updateEmployee")
    @ResponseBody
    public Map<String, String> updateEmployee(Employee employee) {
        int i = employeeService.updateEmployee(employee);
        Map<String, String> map = new HashMap<>();
        if (i != 0) {
            map.put("msg", "ok");
        } else {
            map.put("msg", "no");

        }
        return map;
    }

    @RequestMapping("/delEmployee")
    public String delEmployee(Integer pageNumber, Integer pageSize, ModelMap modelMap,Employee empId) {
       employeeService.deleteEmployee(empId);
        int number;
        int size;
        if (pageNumber == null) {
            number = 1; //默认显示第1页
        } else {
            number = pageNumber;
        }
        if (pageSize == null) {
            size = 5; //默认每页显示两条
        } else {
            size = pageSize;
        }
        //调用服务层换回模型数据
        PageData<Employee> pageData = employeeService.getEmployeeByPage(number, size);
        //给视图传递上下文参数
        modelMap.put("EMPLOYEE", pageData);
        return "employee/list";
    }

    //获取员工信息及实现分页功能
   @RequestMapping("/getEmployee")
    public String getAllUserByPage(Integer pageNumber, Integer pageSize, ModelMap modelMap) {
        //处理参数
        int number;
        int size;
        if (pageNumber == null) {
            number = 1; //默认显示第1页
        } else {
            number = pageNumber;
        }
        if (pageSize == null) {
            size = 5; //默认每页显示两条
        } else {
            size = pageSize;
        }
        //调用服务层换回模型数据
        PageData<Employee> pageData = employeeService.getEmployeeByPage(number, size);
        //给视图传递上下文参数
        modelMap.put("EMPLOYEE", pageData);
        return "employee/list";
    }
}
